/* eslint-disable one-var */
// 判断滚动条的位置
export const getScrollTop = () => {
  let scrollTop = 0
  let bodyScrollTop = 0
  let documentScrollTop = 0
  if (document.body) {
    bodyScrollTop = document.body.scrollTop
  }
  if (document.documentElement) {
    documentScrollTop = document.documentElement.scrollTop
  }
  scrollTop =
    bodyScrollTop - documentScrollTop > 0 ? bodyScrollTop : documentScrollTop
  return scrollTop
}

// 文本加密
export const encryptText = text => {
  let b64pad = ''
  try {
  } catch (e) {
    b64pad = ''
  }
  let tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  let output = ''
  let len = text.length
  for (let i = 0; i < len; i += 3) {
    let triplet =
      (text.charCodeAt(i) << 16) |
      (i + 1 < len ? text.charCodeAt(i + 1) << 8 : 0) |
      (i + 2 < len ? text.charCodeAt(i + 2) : 0)
    for (let j = 0; j < 4; j++) {
      if (i * 8 + j * 6 > text.length * 8) {
        output += b64pad
      } else {
        output += tab.charAt((triplet >>> (6 * (3 - j))) & 63)
      }
    }
  }
  return output
}

// 时间戳转换
export const timestampToTime = (timestamp, boolean) => {
  let time = timestamp
  if (String(timestamp).length === 10) {
    return timestamp * 1000
  } else {
  }
  if (time.length && navigator.userAgent.indexOf('Safari') === -1) {
    // eslint-disable-next-line no-useless-escape
    time = time.replace(/\-/g, '/')
  }
  let date = new Date(time) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let Y = date.getFullYear() + '-'
  let M =
    (date.getMonth() + 1 < 10
      ? '0' + (date.getMonth() + 1)
      : date.getMonth() + 1) + '-'
  let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
  let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
  let m =
    (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
  let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  if (!boolean) {
    return Y + M + D + ' ' + h + m + s
  } else {
    return Y + M + D
  }
}

// 计算字符串字节数
export const strCharLength = (str) => {
  return str.replace(/[^u4e00-u9fa5]/g, 'aa').length
}
// 价格格式化
export const fmoney = (s) => {
  if (!s) {
    return '0.00'
  }
  // eslint-disable-next-line no-useless-escape
  s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(2) + ''
  let l = s.split('.')[0].split('').reverse(),
    r = s.split('.')[1],
    t = ''
  for (let i = 0; i < l.length; i++) {
    t += l[i] + ((i + 1) % 3 === 0 && (i + 1) !== l.length ? ',' : '')
  }
  return t.split('').reverse().join('') + '.' + r
}
